<?php

class GeanyController extends Controller {

    protected $mUserInformation;            //User Model
    protected $mDictPrivilegeInfo=NULL;     //List Of PrivilegeInfo
    protected $mFunctionInformationId;        //Fungsi dari controller
    protected $mFunctionInformationName;        //Nama dari Fungsi
    
    function __construct(){
        $this->loadLoginInfo();
        $this->loadFunctionInfoList();
    }
    
    protected function setupLayout(){
        if ( ! is_null($this->layout)){
            $this->layout = View::make($this->layout);
        }
    }
    
    protected function isLogin(){
        return Auth::check();
    }
    
    protected function isAllowAccess(){
        $hasAccess = false;
        if($this->mUserInformation!=NULL){
            $privilegeInfoService = new PrivilegeInfoService();
            $privilegeInfoList = $privilegeInfoService->getPrivilegeInfoByGroupId($this->mUserInformation->getGroup()->getId());
            if($privilegeInfoList!=null){
                foreach($privilegeInfoList as $item){
                    if($item->getFunctions()!=null&&$item->getFunctions()->getId()==$this->mFunctionInformationId) $hasAccess = true;
                }
            }
        }
        return $hasAccess;
    }
    
    private function loadLoginInfo() {
        if ($this->isLogin()) {
            $AuthUser = Auth::user();
            $UserService = new UserService();
            $this->mUserInformation = $UserService->getObject($AuthUser->id);
            
           View::share('userInformation', $this->mUserInformation);
        } else {
            View::share('userInformation', new User());
        }
    }
    
    protected function userInformation(){
        return $this->mUserInformation;
    }
    
    private function loadFunctionInfoList(){
        if($this->mUserInformation==NULL) return false;
        $privilegeInfoService = new PrivilegeInfoService();
        $privilegeInfoList = $privilegeInfoService->getPrivilegeInfoByGroupId($this->mUserInformation->getGroup()==NULL ? "" : $this->mUserInformation->getGroup()->getId());
        if($privilegeInfoList!=null){
            foreach($privilegeInfoList as $item){
                $module = $item->getModule();
                $functions = $item->getFunctions();
                if($module!=null&&$functions!=null){
                    if($module!=null&&$functions->getIsShow()!=false){
                        if(!isset($this->mDictPrivilegeInfo[$module->getName()])) {
                            $this->mDictPrivilegeInfo['module'][] = $module;
                            $this->mDictPrivilegeInfo[$module->getName()] = array();
                        }
                            $this->mDictPrivilegeInfo[$module->getName()][] = $functions;
                            
                    }
                }
            }
        }
        View::share('mDictPrivilegeInfo', $this->mDictPrivilegeInfo);
    }
    
    protected function setFunctionId($value) {
        $this->mFunctionInformationId = $value;
        $functionService = new FunctionsService();
        $obj_function = $functionService->getObject($this->mFunctionInformationId);
        if ($obj_function != null) $this->mFunctionInformationName = $obj_function->getName();
    }
    
    protected function getFunctionName() {
        return $this->mFunctionInformationName;
    }
    
    protected function functionInformation($functionInformation=NULL){
        if ($functionInformation==NULL) return $this->mFunctionInformation;
        $this->mFunctionInformation = $functionInformation;
    }
    
    protected function addModelError($key, $err=null) {
        PageData::addModelError($key,$err);
    }
	
    protected function assignToModelState($errors) {
        PageData::assignToModelState($errors);
    }
    
    protected function assignValidatorToModelState($validator){
        PageData::assignValidatorToModelState($validator);
    }
    
}
        
